﻿using BaseService.Systems.MenuManagement.Dto;
using Business.BorrowManagement;
using Business.BorrowManagement.Dto;
using Business.Models;
using Business.SendManagement.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using XCZ;
using Yitter.IdGenerator;

namespace Business.SendManagement
{
    public class SendAppService : BusinessWorkFlowAppService, ISendAppService
    {
        private const string FormName = "Send";
        private IRepository<SendG, Guid> _repository;
        private IRepository<SendDetail, Guid> _repository1;




        public SendAppService(
            IRepository<SendG, Guid> repository, IRepository<SendDetail, Guid> repository1
            )
        {
            _repository = repository;
            _repository1 = repository1;
        }
        public async Task<SendDto> Get(Guid id)
        {
            var query = (from b in await _repository.GetQueryableAsync()
                         where id == b.Id
                         select new SendDto
                         {
                             Id = b.Id,
                             STitle = b.STitle,
                             SNum = b.SNum,
                             STime = b.STime,
                             APeo = b.APeo,
                             ProPeo = b.ProPeo,
                             RPeo = b.RPeo,
                             RAdss = b.RAdss,
                             Phone = b.Phone,
                             ProKeepId = b.ProKeepId,
                             SIPeo = b.SIPeo,
                             SITime = b.SITime,
                             SStyle = b.SStyle,
                             SCom = b.SCom,
                             SQNum = b.SQNum,
                             SPrice = b.SPrice,
                             SDesc = b.SDesc,
                             createOrUpdateSendDto = (from d in _repository1.GetQueryableAsync().Result
                             where b.Id == d.SendGId
                                                      select new CreateOrUpdateSendDto
                                                      {
                                                          DName = d.DName,
                                                          DNum = d.DNum,
                                                          DKNum = d.DKNum,
                                                          DPrice = d.DPrice,
                                                          DPNum = d.DPNum,
                                                          DPH = d.DPH,
                                                          STime = d.STime,
                                                          SATime = d.SATime,
                                                          SGTime = d.SGTime,
                                                          SPage = d.SPage,
                                                          SDesc = d.SDesc,
                                                          SIsta = d.SIsta,

                                                      }
                             ).ToList()
                         }
                   ).ToList();



            //var data = await _repository.GetAsync(id);
            //var dto = ObjectMapper.Map<BorrowM, BorrowDto>(data);
            return query.ToList()[0];
        }
        /// <summary>
        /// 全部
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<SendDto>> GetAll(GetSendInputDto input)
        {
            //var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), a => a.BTitle.Contains(input.Filter));

            
            var query = (from b in await _repository.GetQueryableAsync()
                       
                         select new SendDto
                         {
                             Id = b.Id,
                             STitle = b.STitle,
                             SNum = b.SNum,
                             SIsta = b.SIsta,
                             STime = b.STime,
                             APeo = b.APeo,
                             ProPeo = b.ProPeo,
                             RPeo = b.RPeo,
                             RAdss = b.RAdss,
                             Phone = b.Phone,
                             ProKeepId = b.ProKeepId,
                             SIPeo = b.SIPeo,
                             SITime = b.SITime,
                             SStyle = b.SStyle,
                             SCom = b.SCom,
                             SQNum = b.SQNum,
                             SPrice = b.SPrice,
                             SDesc = b.SDesc,
                             createOrUpdateSendDto = (from d in _repository1.GetQueryableAsync().Result
                                                      where b.Id == d.SendGId
                                                      select new CreateOrUpdateSendDto
                                                      {
                                                          DName = d.DName,
                                                          DNum = d.DNum,
                                                          DKNum = d.DKNum,
                                                          DPrice = d.DPrice,
                                                          DPNum = d.DPNum,
                                                          DPH = d.DPH,
                                                          STime = d.STime,
                                                          SATime = d.SATime,
                                                          SGTime = d.SGTime,
                                                          SPage = d.SPage,
                                                          SDesc = d.SDesc,
                                                          SIsta = d.SIsta,

                                                      }
                             ).ToList()
                         }
                    ).ToList();

            var totalCount = query.Count();
            var items = query.Skip(input.SkipCount - 1).Take(input.MaxResultCount).ToList();
            //var dtos = ObjectMapper.Map<List<BorrowM>, List<BorrowDto>>(items);
            return new PagedResultDto<SendDto>(totalCount, query);
        }
        /// <summary>
        /// 未发货
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<SendDto>> GetAllNGood(GetSendInputDto input)
        {
            //var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), a => a.BTitle.Contains(input.Filter));


            var query = (from b in await _repository.GetQueryableAsync()
                         where b.SIsta == true
                         select new SendDto
                         {
                             Id = b.Id,
                             STitle = b.STitle,
                             SIsta=b.SIsta,
                             SNum = b.SNum,
                             STime = b.STime,
                             APeo = b.APeo,
                             ProPeo = b.ProPeo,
                             RPeo = b.RPeo,
                             RAdss = b.RAdss,
                             Phone = b.Phone,
                             ProKeepId = b.ProKeepId,
                             SIPeo = b.SIPeo,
                             SITime = b.SITime,
                             SStyle = b.SStyle,
                             SCom = b.SCom,
                             SQNum = b.SQNum,
                             SPrice = b.SPrice,
                             SDesc = b.SDesc,
                             createOrUpdateSendDto = (from d in _repository1.GetQueryableAsync().Result
                                                      where b.Id == d.SendGId
                                                      select new CreateOrUpdateSendDto
                                                      {
                                                          DName = d.DName,
                                                          DNum = d.DNum,
                                                          DKNum = d.DKNum,
                                                          DPrice = d.DPrice,
                                                          DPNum = d.DPNum,
                                                          DPH = d.DPH,
                                                          STime = d.STime,
                                                          SATime = d.SATime,
                                                          SGTime = d.SGTime,
                                                          SPage = d.SPage,
                                                          SDesc = d.SDesc,
                                                          SIsta = d.SIsta,

                                                      }
                             ).ToList()
                         }
                    ).ToList();

            var totalCount = query.Count();
            var items = query.Skip(input.SkipCount - 1).Take(input.MaxResultCount).ToList();
            //var dtos = ObjectMapper.Map<List<BorrowM>, List<BorrowDto>>(items);
            return new PagedResultDto<SendDto>(totalCount, query);
        }
        public async Task<PagedResultDto<SendDto>> GetAllGood(GetSendInputDto input)
        {
            var query = (from b in await _repository.GetQueryableAsync()
                         where b.SIsta == false
                         select new SendDto
                         {
                             Id = b.Id,
                             STitle = b.STitle,
                             SIsta = b.SIsta,
                             SNum = b.SNum,
                             STime = b.STime,
                             APeo = b.APeo,
                             ProPeo = b.ProPeo,
                             RPeo = b.RPeo,
                             RAdss = b.RAdss,
                             Phone = b.Phone,
                             ProKeepId = b.ProKeepId,
                             SIPeo = b.SIPeo,
                             SITime = b.SITime,
                             SStyle = b.SStyle,
                             SCom = b.SCom,
                             SQNum = b.SQNum,
                             SPrice = b.SPrice,
                             SDesc = b.SDesc,
                             createOrUpdateSendDto = (from d in _repository1.GetQueryableAsync().Result
                                                      where b.Id == d.SendGId
                                                      select new CreateOrUpdateSendDto
                                                      {
                                                          DName = d.DName,
                                                          DNum = d.DNum,
                                                          DKNum = d.DKNum,
                                                          DPrice = d.DPrice,
                                                          DPNum = d.DPNum,
                                                          DPH = d.DPH,
                                                          STime = d.STime,
                                                          SATime = d.SATime,
                                                          SGTime = d.SGTime,
                                                          SPage = d.SPage,
                                                          SDesc = d.SDesc,
                                                          SIsta = d.SIsta,

                                                      }
                             ).ToList()
                         }
                    ).ToList();

            var totalCount = query.Count();
            var items = query.Skip(input.SkipCount - 1).Take(input.MaxResultCount).ToList();
            //var dtos = ObjectMapper.Map<List<BorrowM>, List<BorrowDto>>(items);
            return new PagedResultDto<SendDto>(totalCount, query);
        }
        public async Task<SendDto> CreateOrUpdate(SendDto input)
        {
            SendG result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                //雪花Id
                input.SNum = YitIdHelper.NextId().ToString();
                SendG borrow = new SendG()
                {
                    STitle = input.STitle,
                    SNum = input.SNum,
                    STime = input.STime,
                    APeo = input.APeo,
                    ProPeo = input.ProPeo,
                    RPeo = input.RPeo,
                    RAdss = input.RAdss,
                    Phone = input.Phone,
                    ProKeepId = input.ProKeepId,
                    SIPeo = input.SIPeo,
                    SITime = input.SITime,
                    SStyle = input.SStyle,
                    SCom = input.SCom,
                    SQNum = input.SQNum,
                    SPrice = input.SPrice,
                    SDesc = input.SDesc,

                };
                foreach (var item in input.createOrUpdateSendDto)
                {
                    SendDetail detail = new SendDetail()
                    {
                        SendGId = input.Id,
                        DName = item.DName,
                        DNum = item.DNum,
                        DKNum = item.DKNum,
                        DPrice = item.DPrice,
                        DPNum = item.DPNum,
                        DPH = item.DPH,
                        STime = item.STime,
                        SATime = item.SATime,
                        SGTime = item.SGTime,
                        SPage = item.SPage,
                        SDesc = item.SDesc,
                        SIsta = item.SIsta,
                    };
                    await _repository1.InsertAsync(detail);

                };
                result = await _repository.InsertAsync(ObjectMapper.Map<SendDto, SendG>(input));

                //创建工作流
                // await WorkFlow.CreateWorkFlow(FormName, result);
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);

                //BorrowM borrow = new BorrowM()
                //{
                //    BTitle = input.BTitle,
                //    BNum = input.BNum,
                //    BPeo = input.BPeo,
                //    BTme = input.BTme,
                //    BHand = input.BHand,
                //    BCon = input.BCon

                //};
                //borrow=await _repository.UpdateAsync(borrow);
                //foreach (var item in input.borrowDetails)
                //{
                //    BorrowPro detail = new BorrowPro()
                //    {
                //        BorrowId =(Guid) input.Id,
                //        PName = item.PName,
                //        PNum = item.PNum,
                //        SNum = item.SNum,
                //        BUnit = item.BUnit,
                //        BorNum = item.BorNum,
                //        GoBit = item.GoBit,
                //        BitInfo = item.BitInfo,
                //        GiveTime = item.GiveTime,
                //        PageStyle = item.PageStyle,
                //        Desc = item.Desc
                //    };
                //    await _repository1.UpdateAsync(detail);

                //};


                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
                //检查流程状态
                //await WorkFlow.CheckFormStatus(input.Id.Value, result);
            }
            return ObjectMapper.Map<SendG, SendDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }
    }
}
